package com.zzh.StudentManagementSystem;



import java.util.Scanner;

public class Main {
    private static void addStudentLoop(Scanner scanner, ArrayListStudent sms) {
        while (true) {
            String id;
            do {
                System.out.println("请输入学生学号：");
                id = scanner.nextLine();
                if (sms.getIndex(id) != -1) {
                    System.out.println("该学号已存在，请重新输入！");
                }
            } while (sms.getIndex(id) != -1);

            System.out.println("输入学生姓名：");
            String name = scanner.nextLine();

            System.out.println("输入学生年龄：");
            int age = Integer.parseInt(scanner.nextLine());

            System.out.println("请输入学生居住地：");
            String address = scanner.nextLine();

            sms.addStudent(new Student1(id, name, age, address));
            System.out.print("如需继续添加请按y，如不继续添加请按任意键退出");
            String continueChoice = scanner.nextLine();
            if (!continueChoice.equalsIgnoreCase("y")) {
                break;
            }
        }
    }
    private static void deleteStudentLoop(Scanner scanner, ArrayListStudent sms) {
        while (true) {
            System.out.print("请输入要删除的学生学号：");
            String deleteId = scanner.nextLine();
            sms.deleteStudent(deleteId);
            System.out.print("如需继续删除请按y，如不继续删除请按任意键退出");
            String continueChoice = scanner.nextLine();
            if (!continueChoice.equalsIgnoreCase("y")) {
                break;
            }
        }
    }

    private static void updateStudentLoop(Scanner scanner, ArrayListStudent sms) {
        while (true) {
            String  id;
            do {
                System.out.println("请输入学生学号：");
                id = scanner.nextLine();
                if (sms.getIndex(id) == -1) {
                    System.out.println("该学号不存在，请重新输入！");
                }
            }
                while (sms.getIndex(id) != -1) ;
                System.out.println("输入要修改的学生学号：");
                String updateId = scanner.nextLine();
                System.out.println("输入新姓名：");
                String newName = scanner.nextLine();
                System.out.println("输入新年龄：");
                int newAge = scanner.nextInt();
                scanner.nextLine();
                System.out.println("请输入新住址：");
                String newAddress = scanner.nextLine();
                sms.updateStudent(updateId, newName, newAge, newAddress);
                System.out.print("如需继续修改请按y，如不继续修改请按任意键退出");
                String continueChoice = scanner.nextLine();
                if (!continueChoice.equalsIgnoreCase("y")) {
                    break;
                }
            }
    }
    private static void findStudentByIdLoop(Scanner scanner, ArrayListStudent sms) {
        while (true) {
            System.out.print("请输入要查询的学生学号：");
            String findId = scanner.nextLine();
            sms.findStudentById(findId);
            System.out.print("如需继续查询请按y，如不继续查询请按任意键退出");
            String continueChoice = scanner.nextLine();
            if (!continueChoice.equalsIgnoreCase("y")) {
                break;
            }
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayListStudent sms = new ArrayListStudent();

        while (true) {
            System.out.println("\n==================== 学生管理系统 ====================");
            System.out.println("                    1. 添加学生");
            System.out.println("                    2. 删除学生");
            System.out.println("                    3. 修改学生信息");
            System.out.println("                    4. 查询学生");
            System.out.println("                    5. 查看所有学生");
            System.out.println("                    6. 退出系统");
            System.out.print("请输入您的选择：");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addStudentLoop(scanner, sms);
                    break;
//                    String id;
//                    do {
//                        System.out.print("请输入学生学号：");
//                        id = scanner.nextLine();
//                        if (sms.getIndex(id) != -1) {
//                            System.out.println("该学号已存在，请重新输入！");
//                        }
//                    } while (sms.getIndex(id) != -1);
//                    System.out.println("输入学生姓名：");
//                    String name = scanner.nextLine();
//                    System.out.println("输入学生年龄：");
//                    int age = scanner.nextInt();
//                    scanner.nextLine();
//                    System.out.println("请输入学生居住地：");
//                    String address = scanner.nextLine();
//                    sms.addStudent(new Student1(id, name, age, address));

//                    break;

//                    System.out.println("输入学生学号：");
//                    String id = scanner.nextLine();
//                    int index = sms.getIndex(id);
//                    if(index == 1){
//                        System.out.println("您输入的学号已经被占用，请重新输入");
//
//                    }
//                    else {
//                        System.out.println("输入学生姓名：");
//                        String name = scanner.nextLine();
//                        System.out.println("输入学生年龄：");
//                        int age = scanner.nextInt();
//                        scanner.nextLine();
//                        System.out.println("请输入学生居住地：");
//                        String address = scanner.nextLine();
//                        sms.addStudent(new com.zzh.StudentManagementSystem.Student1(id, name, age, address));
//                    }
//                    break;

                case 2:
                    deleteStudentLoop(scanner, sms);
                    break;
//                    System.out.print("输入要删除的学生学号：");
//                    String deleteId = scanner.nextLine();
//                    sms.deleteStudent(deleteId);
//                    break;

                case 3:
                    updateStudentLoop(scanner, sms);
                    break;

                case 4:
                    findStudentByIdLoop(scanner, sms);
                    break;
//                    System.out.print("输入要查询的学生学号：");
//                    String findId = scanner.nextLine();
//                    sms.findStudentById(findId);
//                    break;

                case 5:
                    sms.displayAllStudents();
                    break;


                case 6:
                    System.out.println("感谢使用学生管理系统！");
                    scanner.close();
                    System.exit(0);

                default:
                    System.out.println("无效的输入，请重新选择！");
            }
        }
    }

}
